package com.clickhouse_test.app.service;

import com.clickhouse_test.app.bean.ResultSelectMsg;
import com.clickhouse_test.app.mapper.clickhouse.ClickhouseMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.json.JSONObject;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class ClickhouseLogService {
    private final ClickhouseMapper clickhouseMapper;

    @Autowired
    public ClickhouseLogService(ClickhouseMapper clickhouseMapper) {
        this.clickhouseMapper = clickhouseMapper;
    }

    public String getClickhouse(int page, int pageSize){
        List<Map<String, String>> res = new ArrayList<>();
        PageInfo<Map<String, String>> info = null;
        String success = "true";
        String error = "查询成功";
        try {
            PageHelper.startPage(page, pageSize);
            res = clickhouseMapper.getClickhouse();
            info = new PageInfo<>(res);
        } catch (Exception e) {
            success = "false";
            error = "查询失败！";
            System.err.println(e);
        }
        assert info != null;
        return getMsg((int)info.getTotal(), success, res, error, pageSize);
    }

    public String getLog_inspection_journal_column(String CATALOG_CODE, String INSPECTION_DATE){
        List<Map<String, String>> res = new ArrayList<>();
        String success = "true";
        String error = "查询成功";
        try {
            res = clickhouseMapper.getLog_inspection_journal_column(CATALOG_CODE, INSPECTION_DATE);
        } catch (Exception e) {
            success = "false";
            error = "查询失败！";
            System.err.println(e);
        }
        return getMsg(res.size(), success, res, error);
    }

    public String getLogData_uuid(int page, int pageSize, String CATALOG_CODE, String INSPECTION_DATE, String ERROR_CODE, String FIELD_NAME){
        List<Map<String, String>> res = new ArrayList<>();
        PageInfo<Map<String, String>> info = null;
        String success = "true";
        String error = "查询成功";
        try {
            PageHelper.startPage(page, pageSize);
            res = clickhouseMapper.getLogData_uuid(CATALOG_CODE, INSPECTION_DATE, ERROR_CODE, FIELD_NAME);
            info = new PageInfo<>(res);
        } catch (Exception e) {
            success = "false";
            error = "查询失败！";
            System.err.println(e);
        }
        assert info != null;
        return getMsg((int)info.getTotal(), success, res, error, pageSize);
    }

    private String getMsg(int size, String success, List<Map<String, String>> res, String error, int pageSize) {
        ResultSelectMsg resultSelectMsg = new ResultSelectMsg();
        resultSelectMsg.setTotalRowCount(size);
        resultSelectMsg.setSuccess(success);
        resultSelectMsg.setRows(res);
        resultSelectMsg.setError(error);
        resultSelectMsg.setPageSize(pageSize);
        JSONObject result = new JSONObject(resultSelectMsg.getMsg());
        return result.toString();
    }

    private String getMsg(int size, String success, List<Map<String, String>> res, String error) {
        ResultSelectMsg resultSelectMsg = new ResultSelectMsg();
        resultSelectMsg.setTotalRowCount(size);
        resultSelectMsg.setSuccess(success);
        resultSelectMsg.setRows(res);
        resultSelectMsg.setError(error);
        resultSelectMsg.setPageSize(1);
        JSONObject result = new JSONObject(resultSelectMsg.getMsg());
        return result.toString();
    }
}
